﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Resources;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Converter;
using csla4Skeleton.UI.SL.Resources.Utility;
using csla4Skeleton.UI.SL.NghiepVu.TaiSanCoDinh.DM_DoiTuongTHCP;
using csla4Skeleton.UI.SL.DanhMuc.DM_HangHoa;
using csla4Skeleton.UI.SL.NghiepVu.TinhGiaThanh.GT_KyGiaThanh;
using csla4Skeleton.UI.SL.DanhMuc.DM_TaiKhoan;
using csla4Skeleton.UI.SL.NghiepVu.TinhGiaThanh.GT_CPPhanBo;
using csla4Skeleton.UI.SL.NghiepVu.TinhGiaThanh.GT_CPTrucTiep;
using Accounting.Library;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;
using Telerik.Windows;
using System.ComponentModel;
using Utility;
using Accounting.Library.Utility;

namespace csla4Skeleton.UI.SL.NghiepVu.TinhGiaThanh.GT_KetChuyenCP
{
    public partial class GT_KetChuyenCP_MainWindow: UserControl, Csla.Xaml.ISupportNavigation
    {

        #region ISupportNavigation Members
        List<Control> controls = new List<Control>();
        public bool CreateBookmarkAfterLoadCompleted
        {
            get { return true; }
        }

        public event EventHandler LoadCompleted;

        protected virtual void OnLoadCompleted()
        {
            if (LoadCompleted != null)
                LoadCompleted(this, EventArgs.Empty);
        }

        public void SetParameters(string parameters)
        {
            try
            {
                if (parameters == "")
                    Khoa = null;
                else
                    Khoa = Int32.Parse(parameters);
            }
            catch
            {
                Khoa = null;
            }
        }

        public string Title
        {
            get { return "KetChuyenCP"; }
        }

       
        #endregion

        #region Load Methods
        private System.Nullable<int> Khoa = null;
      
        public GT_KetChuyenCP_MainWindow(int Khoa)          
        {
            this.Khoa = Khoa;
           
        }

        public GT_KetChuyenCP_MainWindow()
        {
            LocalizationConverter converter = new LocalizationConverter();
            converter.LocalResourceManager = new ResourceManager("csla4Skeleton.UI.SL.NghiepVu.TinhGiaThanh.GT_KetChuyenCP.Resources", Assembly.GetExecutingAssembly());
            this.Resources.Add("Localization", converter);
            
            InitializeComponent();  
            ControlBehavior.ControlBehaviors.Navigation(this.GT_KetChuyenCP_ChungTuInformation_GroupBox, controls);
           
           
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            {
                
                System.Windows.Data.CollectionViewSource GT_KetChuyenCP_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["GT_KetChuyenCP_ViewModelSource"];
                if (Khoa == null)
                {
                    GT_KetChuyenCP_ViewModel GT_KetChuyenCP_ViewModel = new GT_KetChuyenCP_ViewModel();
                    GT_KetChuyenCP_ViewModel.ChildList_AddedNew += new GT_KetChuyenCP_ViewModel.ChildList_AddedNew_EventHandler(GT_KetChuyenCP_ViewModel_ChildList_AddedNew);
                    GT_KetChuyenCP_ViewModel.KyGiaThanhIdChanged += new EventHandler(OnKyGiaThanhIdChanged);
                    GT_KetChuyenCP_CollectionViewSource.Source = new List<object> { GT_KetChuyenCP_ViewModel };

                    
                }
                else
                {
                    GT_KetChuyenCP_ViewModel GT_KetChuyenCP_ViewModel = new GT_KetChuyenCP_ViewModel((int)Khoa);
                    GT_KetChuyenCP_ViewModel.ChildList_AddedNew += new GT_KetChuyenCP_ViewModel.ChildList_AddedNew_EventHandler(GT_KetChuyenCP_ViewModel_ChildList_AddedNew);
                    GT_KetChuyenCP_ViewModel.KyGiaThanhIdChanged += new EventHandler(OnKyGiaThanhIdChanged);
                    GT_KetChuyenCP_CollectionViewSource.Source = new List<object> { GT_KetChuyenCP_ViewModel };

                    
                }

                System.Windows.Data.CollectionViewSource dm_DoiTuongTHCP_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_DoiTuongTHCP_ListViewModelSource"];
                DM_DoiTuongTHCP_ListViewModel dm_DoiTuongTHCP_ListViewModel = new DM_DoiTuongTHCP_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_DoiTuongTHCP_CollectionViewSource.Source = new List<object> { dm_DoiTuongTHCP_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_TaiKhoan_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoan_ListViewModelSource"];
                DM_TaiKhoan_ListViewModel dm_TaiKhoan_ListViewModel = new DM_TaiKhoan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_TaiKhoan_CollectionViewSource.Source = new List<object> { dm_TaiKhoan_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_HangHoa_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_HangHoa_ListViewModelSource"];
                DM_HangHoa_ListViewModel dm_HangHoa_ListViewModel = new DM_HangHoa_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_HangHoa_CollectionViewSource.Source = new List<object> { dm_HangHoa_ListViewModel };

                System.Windows.Data.CollectionViewSource gt_CPTrucTiep_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["GT_CPTrucTiep_ListViewModelSource"];
                GT_CPTrucTiep_ListViewModel gt_CPTrucTiep_ListViewModel = new GT_CPTrucTiep_ListViewModel(BusinessConfiguration.Configuration.CPTrucTiepFilterMode);
                gt_CPTrucTiep_CollectionViewSource.Source = new List<object> { gt_CPTrucTiep_ListViewModel };

                System.Windows.Data.CollectionViewSource gt_CPPhanBo_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["GT_CPPhanBo_ListViewModelSource"];
                GT_CPPhanBo_ListViewModel gt_CPPhanBo_ListViewModel = new GT_CPPhanBo_ListViewModel(BusinessConfiguration.Configuration.CPPhanBoFilterMode);
                gt_CPPhanBo_CollectionViewSource.Source = new List<object> { gt_CPPhanBo_ListViewModel };
              
                System.Windows.Data.CollectionViewSource gt_KyGiaThanh_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["GT_KyGiaThanh_ListViewModelSource"];
                GT_KyGiaThanh_ListViewModel gt_KyGiaThanh_ListViewModel = new GT_KyGiaThanh_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                gt_KyGiaThanh_CollectionViewSource.Source = new List<object> { gt_KyGiaThanh_ListViewModel };

                System.Windows.Data.CollectionViewSource gt_KyGiaThanh_ChiTiet_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["GT_KyGiaThanh_ChiTiet_ListViewModelSource"];
                GT_KyGiaThanh_ListViewModel gt_KyGiaThanh_ChiTiet_ListViewModel = new GT_KyGiaThanh_ListViewModel(BusinessConfiguration.Configuration.KyGiaThanhFilterMode);
                gt_KyGiaThanh_ChiTiet_CollectionViewSource.Source = new List<object> { gt_KyGiaThanh_ChiTiet_ListViewModel };

                this.GT_KetChuyenCP_KetChuyenCPChiTietGridView.KeyboardCommandProvider = new ControlBehavior.GridViewKeyboardCommandProvider(this.GT_KetChuyenCP_KetChuyenCPChiTietGridView);
                
                ((ControlBehavior.GridViewKeyboardCommandProvider)this.GT_KetChuyenCP_KetChuyenCPChiTietGridView.KeyboardCommandProvider).ReachLastColumn += new ControlBehavior.GridViewKeyboardCommandProvider.ReachLastColumn_EventHandler(OnReachLastColumn);

                ((ControlBehavior.GridViewKeyboardCommandProvider)this.GT_KetChuyenCP_KetChuyenCPChiTietGridView.KeyboardCommandProvider).TabKeyPress += new EventHandler(OnTabKeyPress);
            }
        }

        #endregion
       

        #region Event Handle
        private void LayoutRoot_KeyUp(object sender, KeyEventArgs e)
        {
            if (Keyboard.Modifiers == ModifierKeys.Control)
            {
                GT_KetChuyenCP_ViewModel vm = GetViewModel();
                if (e.Key == Key.S)
                {
                    if (vm.CanSave)
                    {
                        vm.Save(sender, new Csla.Xaml.ExecuteEventArgs());
                    }
                }
                else if (e.Key == Key.F6)
                {
                    AddItems();
                }
                else if (e.Key == Key.D1)
                {
                    this.GT_KetChuyenCP_KetChuyenCPChiTietTab.IsSelected = true;
                }
               
               
                
            }
            e.Handled = true;
        }

        void AddItemButton_Click(object sender, RoutedEventArgs e)
        {

            AddItems();
        }

        void DeleteItemButton_Click(object sender, RoutedEventArgs e)
        {
            DeleteItems();
          
        }

        public void AddItems()
        {
            if (this.GT_KetChuyenCP_KetChuyenCPChiTietTab.IsSelected)
            {
                AddKetChuyenCPDetailsItem();
            }
        }
        private RadGridView GetActiveGridView()
        {
            if (this.GT_KetChuyenCP_KetChuyenCPChiTietTab.IsSelected)
            {
                return GT_KetChuyenCP_KetChuyenCPChiTietGridView;
            }
           
            return null;
        }



        void DeleteItems()
        {
           if (this.GT_KetChuyenCP_KetChuyenCPChiTietTab.IsSelected)
            {
                GT_KetChuyenCP_ChiTietChild ChiTietChild = (GT_KetChuyenCP_ChiTietChild)(this.GT_KetChuyenCP_KetChuyenCPChiTietGridView.SelectedItem);
                IList<GT_KetChuyenCP_ChiTietChild> list = GetViewModel().Model.GT_KetChuyenCP_ChiTiets;
                int index = list.IndexOf(ChiTietChild);
                DeleteKetChuyenCPDeTailsItem(index);
            }
           
            
        }
           
        void GridView_AddingNewDataItem(object sender, GridViewAddingNewEventArgs e)
        {
            var grid = e.OwnerGridViewItemsControl;
            if (grid.Items.Count > 0)
            {
                GridViewUtil.FocusRow((RadGridView)grid, grid.Items.Count - 1);
                Dispatcher.BeginInvoke(new Action(() =>
                {
                    grid.CurrentCellInfo = new GridViewCellInfo(grid.Items[grid.Items.Count - 1], grid.Columns[0]);
                    grid.Focus();
                    grid.BeginEdit();
                }));
            }
        }
              
        private void SaveAndNewButton_Click(object sender, RoutedEventArgs e)
        {
           GT_KetChuyenCP_ViewModel vm = GetViewModel();
            vm.SaveAndNew();
        }

        private void DatePicker_KeyUp(object sender, KeyEventArgs e)
        {
            List<Control> availableControl = new List<Control>();
            foreach (Control item in controls)
            {
                if (item.Visibility == System.Windows.Visibility.Visible)
                {
                    availableControl.Add(item);
                }
            }
            Control[] list = availableControl.ToArray();
            if (sender.GetHashCode() == list[availableControl.Count - 1].GetHashCode())
            {
                if (e.Key == Key.Enter)
                {
                    this.AddKetChuyenCPDetailsItem();
                }
            }
            else
            {
                ControlBehavior.DatePickerBehaviors.DatePicker_KeyUp(sender, e, controls);
            }
        }

        private void ComboBox_KeyUp(object sender, KeyEventArgs e)
        {
            List<Control> availableControl = new List<Control>();
            foreach (Control item in controls)
            {
                if (item.Visibility == System.Windows.Visibility.Visible)
                {
                    availableControl.Add(item);
                }
            }
            Control[] list = availableControl.ToArray();
            if (sender.GetHashCode() == list[availableControl.Count - 1].GetHashCode())
            {
                if (e.Key == Key.Enter)
                {
                    this.AddKetChuyenCPDetailsItem();
                }
            }
            else
            {
                ControlBehavior.ComboBoxBehaviors.ComboBox_KeyUp(sender, e, controls);
            }
        }

        private void Control_KeyUp(object sender, KeyEventArgs e)
        {
            List<Control> availableControl = new List<Control>();
            foreach (Control item in controls)
            {
                if (item.Visibility == System.Windows.Visibility.Visible)
                {
                    availableControl.Add(item);
                }
            }
            Control[] list = availableControl.ToArray();
            if (sender.GetHashCode() == list[availableControl.Count - 1].GetHashCode())
            {
                if (e.Key == Key.Enter)
                {
                    this.AddKetChuyenCPDetailsItem();
                }
            }
            else
            {
                ControlBehavior.ControlBehaviors.Control_KeyUp(sender, e, controls);
            }
        }


        private void BookedItemButton_Click(object sender, RoutedEventArgs e)
        {
            GetViewModel().GhiSo();
        }

        private void DeleteBookedItemButton_Click(object sender, RoutedEventArgs e)
        {
            GetViewModel().BoGhi();
        }

        void OnTabKeyPress(object sender, EventArgs e)
        {
            this.tabControl.SelectedItem = tabControl.Items[0];
            this.AddKetChuyenCPDetailsItem();
        }


        void OnReachLastColumn(object sender, ReachLastColumnArgs e)
        {

            List<TabItem> AvailableTabItem = new List<TabItem>();
            int indexCurrentTab = 0;
            foreach (TabItem item in this.tabControl.Items)
            {
                if (item.Visibility == System.Windows.Visibility.Visible)
                {
                    AvailableTabItem.Add(item);
                    if (item.GetHashCode() == tabControl.SelectedItem.GetHashCode())
                    {
                        indexCurrentTab = AvailableTabItem.IndexOf(item);
                    }

                }
            }


            TabItem[] list = AvailableTabItem.ToArray();
            if (indexCurrentTab == (AvailableTabItem.Count - 1))
            {
                this.tabControl.SelectedItem = list[0];
                FocusAfterChangeTab(this.GT_KetChuyenCP_KetChuyenCPChiTietGridView, e.Index);

            }
            

        }

        private void FocusAfterChangeTab(GridViewDataControl grid, int index)
        {
             
                Dispatcher.BeginInvoke(new Action(() =>
                {
                    grid.CurrentCellInfo = new GridViewCellInfo(grid.Items[index], grid.Columns[0]);
                    grid.Focus();
                    grid.BeginEdit();
                }));
           
        }
        
        
       
        #endregion

        #region View Model Related Methods
        private GT_KetChuyenCP_ViewModel GetViewModel()
        {
            System.Windows.Data.CollectionViewSource GT_KetChuyenCP_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["GT_KetChuyenCP_ViewModelSource"];
            return ((List<object>)GT_KetChuyenCP_CollectionViewSource.Source)[0] as GT_KetChuyenCP_ViewModel;
        }


        private void AddKetChuyenCPDetailsItem()
        {
            GetViewModel().AddGT_KetChuyenCP_ChiTietItem();
        }
        private void DeleteKetChuyenCPDeTailsItem(int Index)
        {
            GetViewModel().DeleteGT_KetChuyenCP_ChiTietItem( Index);
        }

        void OnKyGiaThanhIdChanged(object sender, EventArgs e)
        {
            
            System.Windows.Data.CollectionViewSource gt_KyGiaThanh_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["GT_KyGiaThanh_ChiTiet_ListViewModelSource"];
            GT_KyGiaThanh_ListViewModel gt_KyGiaThanh_ListViewModel = new GT_KyGiaThanh_ListViewModel(BusinessConfiguration.Configuration.KyGiaThanhFilterMode);
            gt_KyGiaThanh_ListViewModel.LoadCompleted += new EventHandler(OnKyGiaThanhChiTietLoadCompleteted);
            gt_KyGiaThanh_CollectionViewSource.Source = new List<object> { gt_KyGiaThanh_ListViewModel };

        }

        List<List<string>> listTaiKhoan = new List<List<string>>();

        void OnKyGiaThanhChiTietLoadCompleteted(object sender, EventArgs e)
        {
            IList<GT_KetChuyenCP_ChiTietChild> listChiTiet = GetViewModel().Model.GT_KetChuyenCP_ChiTiets;           
            while (listChiTiet.Count != 0)
            {
                this.DeleteKetChuyenCPDeTailsItem(0);
            }
            
            System.Windows.Data.CollectionViewSource GT_KyGiaThanh_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["GT_KyGiaThanh_ChiTiet_ListViewModelSource"];
            GT_KyGiaThanh_ListViewModel gt_KyGiaThanh_ListViewModel = ((List<object>)GT_KyGiaThanh_CollectionViewSource.Source)[0] as GT_KyGiaThanh_ListViewModel;
            IList<GT_KyGiaThanhInfo> listKyGiaThanh = gt_KyGiaThanh_ListViewModel.Model.getListOfInfo();

            System.Windows.Data.CollectionViewSource GT_CPTrucTiep_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["GT_CPTrucTiep_ListViewModelSource"];
            GT_CPTrucTiep_ListViewModel gt_CPTrucTiep_ListViewModel = ((List<object>)GT_CPTrucTiep_CollectionViewSource.Source)[0] as GT_CPTrucTiep_ListViewModel;
            IList<GT_CPTrucTiepInfo> listCPTrucTiep = gt_CPTrucTiep_ListViewModel.Model.getListOfInfo();

            System.Windows.Data.CollectionViewSource GT_CPPhanBo_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["GT_CPPhanBo_ListViewModelSource"];
            GT_CPPhanBo_ListViewModel gt_CPPhanBo_ListViewModel = ((List<object>)GT_CPPhanBo_CollectionViewSource.Source)[0] as GT_CPPhanBo_ListViewModel;
            IList<GT_CPPhanBoInfo> listCPPhanBo = gt_CPPhanBo_ListViewModel.Model.getListOfInfo();

            while (listTaiKhoan.Count != 0)
            {
                listTaiKhoan.RemoveAt(0);
            }


            foreach (GT_KyGiaThanhInfo item in listKyGiaThanh)
            {
                if (item.Khoa == this.GetViewModel().Model.KyGiaThanhId.Value)
                {
                    foreach (GT_CPTrucTiepInfo cpTrucTiepItem in listCPTrucTiep)
                    {
                        if (cpTrucTiepItem.DoiTuongTHCPId == item.DoiTuongTHCPId && cpTrucTiepItem.KyGiaThanhId == item.Khoa
                            && cpTrucTiepItem.HangHoaId == item.HangHoaId)
                        {
                            if (cpTrucTiepItem.Tk_621 != null && cpTrucTiepItem.Tk_621 != 0)
                            {
                                List<string> listitem = new List<string>();
                                listitem.Add(cpTrucTiepItem.DoiTuongTHCPId.ToString());
                                listitem.Add(cpTrucTiepItem.HangHoaId.ToString());
                                listitem.Add(cpTrucTiepItem.KyGiaThanhId.ToString());
                                listitem.Add(cpTrucTiepItem.Tk_621.ToString());
                                listitem.Add("Kết chuyển 621");
                                listTaiKhoan.Add(listitem);
                            }
                            if (cpTrucTiepItem.Tk_622 != null && cpTrucTiepItem.Tk_622 != 0)
                            {
                                List<string> listitem = new List<string>();
                                listitem.Add(cpTrucTiepItem.DoiTuongTHCPId.ToString());
                                listitem.Add(cpTrucTiepItem.HangHoaId.ToString());
                                listitem.Add(cpTrucTiepItem.KyGiaThanhId.ToString());
                                listitem.Add(cpTrucTiepItem.Tk_622.ToString());
                                listitem.Add("Kết chuyển 622");
                                listTaiKhoan.Add(listitem);
                            }

                            if (cpTrucTiepItem.Tk_6231 != null && cpTrucTiepItem.Tk_6231 != 0)
                            {
                                List<string> listitem = new List<string>();
                                listitem.Add(cpTrucTiepItem.DoiTuongTHCPId.ToString());
                                listitem.Add(cpTrucTiepItem.HangHoaId.ToString());
                                listitem.Add(cpTrucTiepItem.KyGiaThanhId.ToString());
                                listitem.Add(cpTrucTiepItem.Tk_6231.ToString());
                                listitem.Add("Kết chuyển 6231");
                                listTaiKhoan.Add(listitem);
                            }

                            if (cpTrucTiepItem.Tk_6232 != null && cpTrucTiepItem.Tk_6232 != 0)
                            {
                                List<string> listitem = new List<string>();
                                listitem.Add(cpTrucTiepItem.DoiTuongTHCPId.ToString());
                                listitem.Add(cpTrucTiepItem.HangHoaId.ToString());
                                listitem.Add(cpTrucTiepItem.KyGiaThanhId.ToString());
                                listitem.Add(cpTrucTiepItem.Tk_6232.ToString());
                                listitem.Add("Kết chuyển 6232");
                                listTaiKhoan.Add(listitem);
                            }
                            if (cpTrucTiepItem.Tk_6233 != null && cpTrucTiepItem.Tk_6233 != 0)
                            {
                                List<string> listitem = new List<string>();
                                listitem.Add(cpTrucTiepItem.DoiTuongTHCPId.ToString());
                                listitem.Add(cpTrucTiepItem.HangHoaId.ToString());
                                listitem.Add(cpTrucTiepItem.KyGiaThanhId.ToString());
                                listitem.Add(cpTrucTiepItem.Tk_6233.ToString());
                                listitem.Add("Kết chuyển 6233");
                                listTaiKhoan.Add(listitem);
                            }
                            if (cpTrucTiepItem.Tk_6234 != null && cpTrucTiepItem.Tk_6234 != 0)
                            {
                                List<string> listitem = new List<string>();
                                listitem.Add(cpTrucTiepItem.DoiTuongTHCPId.ToString());
                                listitem.Add(cpTrucTiepItem.HangHoaId.ToString());
                                listitem.Add(cpTrucTiepItem.KyGiaThanhId.ToString());
                                listitem.Add(cpTrucTiepItem.Tk_6234.ToString());
                                listitem.Add("Kết chuyển 6234");
                                listTaiKhoan.Add(listitem);
                            }
                            if (cpTrucTiepItem.Tk_6237 != null && cpTrucTiepItem.Tk_6237 != 0)
                            {
                                List<string> listitem = new List<string>();
                                listitem.Add(cpTrucTiepItem.DoiTuongTHCPId.ToString());
                                listitem.Add(cpTrucTiepItem.HangHoaId.ToString());
                                listitem.Add(cpTrucTiepItem.KyGiaThanhId.ToString());
                                listitem.Add(cpTrucTiepItem.Tk_6237.ToString());
                                listitem.Add("Kết chuyển 6237");
                                listTaiKhoan.Add(listitem);
                            }
                            if (cpTrucTiepItem.Tk_6238 != null && cpTrucTiepItem.Tk_6238 != 0)
                            {
                                List<string> listitem = new List<string>();
                                listitem.Add(cpTrucTiepItem.DoiTuongTHCPId.ToString());
                                listitem.Add(cpTrucTiepItem.HangHoaId.ToString());
                                listitem.Add(cpTrucTiepItem.KyGiaThanhId.ToString());
                                listitem.Add(cpTrucTiepItem.Tk_6238.ToString());
                                listitem.Add("Kết chuyển 6238");
                                listTaiKhoan.Add(listitem);
                            }
                            if (cpTrucTiepItem.Tk_6271 != null && cpTrucTiepItem.Tk_6271 != 0)
                            {
                                List<string> listitem = new List<string>();
                                listitem.Add(cpTrucTiepItem.DoiTuongTHCPId.ToString());
                                listitem.Add(cpTrucTiepItem.HangHoaId.ToString());
                                listitem.Add(cpTrucTiepItem.KyGiaThanhId.ToString());
                                listitem.Add(cpTrucTiepItem.Tk_6271.ToString());
                                listitem.Add("Kết chuyển 6271");
                                listTaiKhoan.Add(listitem);
                            }
                            if (cpTrucTiepItem.Tk_6272 != null && cpTrucTiepItem.Tk_6272 != 0)
                            {
                                List<string> listitem = new List<string>();
                                listitem.Add(cpTrucTiepItem.DoiTuongTHCPId.ToString());
                                listitem.Add(cpTrucTiepItem.HangHoaId.ToString());
                                listitem.Add(cpTrucTiepItem.KyGiaThanhId.ToString());
                                listitem.Add(cpTrucTiepItem.Tk_6272.ToString());
                                listitem.Add("Kết chuyển 6272");
                                listTaiKhoan.Add(listitem);
                            }
                            if (cpTrucTiepItem.Tk_6273 != null && cpTrucTiepItem.Tk_6273 != 0)
                            {
                                List<string> listitem = new List<string>();
                                listitem.Add(cpTrucTiepItem.DoiTuongTHCPId.ToString());
                                listitem.Add(cpTrucTiepItem.HangHoaId.ToString());
                                listitem.Add(cpTrucTiepItem.KyGiaThanhId.ToString());
                                listitem.Add(cpTrucTiepItem.Tk_6273.ToString());
                                listitem.Add("Kết chuyển 6273");
                                listTaiKhoan.Add(listitem);
                            }
                            if (cpTrucTiepItem.Tk_6274 != null && cpTrucTiepItem.Tk_6274 != 0)
                            {
                                List<string> listitem = new List<string>();
                                listitem.Add(cpTrucTiepItem.DoiTuongTHCPId.ToString());
                                listitem.Add(cpTrucTiepItem.HangHoaId.ToString());
                                listitem.Add(cpTrucTiepItem.KyGiaThanhId.ToString());
                                listitem.Add(cpTrucTiepItem.Tk_6274.ToString());
                                listitem.Add("Kết chuyển 6274");
                                listTaiKhoan.Add(listitem);
                            }
                            if (cpTrucTiepItem.Tk_6277 != null && cpTrucTiepItem.Tk_6277 != 0)
                            {
                                List<string> listitem = new List<string>();
                                listitem.Add(cpTrucTiepItem.DoiTuongTHCPId.ToString());
                                listitem.Add(cpTrucTiepItem.HangHoaId.ToString());
                                listitem.Add(cpTrucTiepItem.KyGiaThanhId.ToString());
                                listitem.Add(cpTrucTiepItem.Tk_6277.ToString());
                                listitem.Add("Kết chuyển 6277");
                                listTaiKhoan.Add(listitem);
                            }
                            if (cpTrucTiepItem.Tk_6278 != null && cpTrucTiepItem.Tk_6278 != 0)
                            {
                                List<string> listitem = new List<string>();
                                listitem.Add(cpTrucTiepItem.DoiTuongTHCPId.ToString());
                                listitem.Add(cpTrucTiepItem.HangHoaId.ToString());
                                listitem.Add(cpTrucTiepItem.KyGiaThanhId.ToString());
                                listitem.Add(cpTrucTiepItem.Tk_6278.ToString());
                                listitem.Add("Kết chuyển 6278");
                                listTaiKhoan.Add(listitem);
                            }

                        }
                    }

                    foreach (GT_CPPhanBoInfo cpPhanBoItem in listCPPhanBo)
                    {
                        if (cpPhanBoItem.DoiTuongTHCPId == item.DoiTuongTHCPId && cpPhanBoItem.KyGiaThanhId == item.Khoa
                            && cpPhanBoItem.HangHoaId == item.HangHoaId)
                        {

                            if (cpPhanBoItem.Tk_621 != null && cpPhanBoItem.Tk_621 != 0)
                            {
                                int result = IsExistInListTaiKhoan(cpPhanBoItem.DoiTuongTHCPId.ToString(), cpPhanBoItem.HangHoaId.ToString(), cpPhanBoItem.KyGiaThanhId.ToString(), "Kết chuyển 621");

                                if (result == -1)
                                {
                                    List<string> listitem = new List<string>();
                                    listitem.Add(cpPhanBoItem.DoiTuongTHCPId.ToString());
                                    listitem.Add(cpPhanBoItem.HangHoaId.ToString());
                                    listitem.Add(cpPhanBoItem.KyGiaThanhId.ToString());
                                    listitem.Add(cpPhanBoItem.Tk_621.ToString());
                                    listitem.Add("Kết chuyển 621");
                                    listTaiKhoan.Add(listitem);

                                }
                                else
                                {
                                    List<string>[] arraylistTaiKhoan = listTaiKhoan.ToArray();
                                    string[] arrayChiTietListTaiKhoan = arraylistTaiKhoan[result].ToArray();
                                    double SoTienMoi = cpPhanBoItem.Tk_621.Value + double.Parse(arrayChiTietListTaiKhoan[3]);
                                    listTaiKhoan.RemoveAt(result);

                                    List<string> listitem = new List<string>();
                                    listitem.Add(cpPhanBoItem.DoiTuongTHCPId.ToString());
                                    listitem.Add(cpPhanBoItem.HangHoaId.ToString());
                                    listitem.Add(cpPhanBoItem.KyGiaThanhId.ToString());
                                    listitem.Add(SoTienMoi.ToString());
                                    listitem.Add("Kết chuyển 621");
                                    listTaiKhoan.Add(listitem);

                                }
                            }

                            if (cpPhanBoItem.Tk_622 != null && cpPhanBoItem.Tk_622 != 0)
                            {
                                int result = IsExistInListTaiKhoan(cpPhanBoItem.DoiTuongTHCPId.ToString(), cpPhanBoItem.HangHoaId.ToString(), cpPhanBoItem.KyGiaThanhId.ToString(), "Kết chuyển 622");

                                if (result == -1)
                                {
                                    List<string> listitem = new List<string>();
                                    listitem.Add(cpPhanBoItem.DoiTuongTHCPId.ToString());
                                    listitem.Add(cpPhanBoItem.HangHoaId.ToString());
                                    listitem.Add(cpPhanBoItem.KyGiaThanhId.ToString());
                                    listitem.Add(cpPhanBoItem.Tk_622.ToString());
                                    listitem.Add("Kết chuyển 622");
                                    listTaiKhoan.Add(listitem);

                                }
                                else
                                {
                                    List<string>[] arraylistTaiKhoan = listTaiKhoan.ToArray();
                                    string[] arrayChiTietListTaiKhoan = arraylistTaiKhoan[result].ToArray();
                                    double SoTienMoi = cpPhanBoItem.Tk_622.Value + double.Parse(arrayChiTietListTaiKhoan[3]);
                                    listTaiKhoan.RemoveAt(result);

                                    List<string> listitem = new List<string>();
                                    listitem.Add(cpPhanBoItem.DoiTuongTHCPId.ToString());
                                    listitem.Add(cpPhanBoItem.HangHoaId.ToString());
                                    listitem.Add(cpPhanBoItem.KyGiaThanhId.ToString());
                                    listitem.Add(SoTienMoi.ToString());
                                    listitem.Add("Kết chuyển 622");
                                    listTaiKhoan.Add(listitem);

                                }
                            }

                            if (cpPhanBoItem.Tk_6231 != null && cpPhanBoItem.Tk_6231 != 0)
                            {
                                int result = IsExistInListTaiKhoan(cpPhanBoItem.DoiTuongTHCPId.ToString(), cpPhanBoItem.HangHoaId.ToString(), cpPhanBoItem.KyGiaThanhId.ToString(), "Kết chuyển 6231");

                                if (result == -1)
                                {
                                    List<string> listitem = new List<string>();
                                    listitem.Add(cpPhanBoItem.DoiTuongTHCPId.ToString());
                                    listitem.Add(cpPhanBoItem.HangHoaId.ToString());
                                    listitem.Add(cpPhanBoItem.KyGiaThanhId.ToString());
                                    listitem.Add(cpPhanBoItem.Tk_6231.ToString());
                                    listitem.Add("Kết chuyển 6231");
                                    listTaiKhoan.Add(listitem);

                                }
                                else
                                {
                                    List<string>[] arraylistTaiKhoan = listTaiKhoan.ToArray();
                                    string[] arrayChiTietListTaiKhoan = arraylistTaiKhoan[result].ToArray();
                                    double SoTienMoi = cpPhanBoItem.Tk_6231.Value + double.Parse(arrayChiTietListTaiKhoan[3]);
                                    listTaiKhoan.RemoveAt(result);

                                    List<string> listitem = new List<string>();
                                    listitem.Add(cpPhanBoItem.DoiTuongTHCPId.ToString());
                                    listitem.Add(cpPhanBoItem.HangHoaId.ToString());
                                    listitem.Add(cpPhanBoItem.KyGiaThanhId.ToString());
                                    listitem.Add(SoTienMoi.ToString());
                                    listitem.Add("Kết chuyển 6231");
                                    listTaiKhoan.Add(listitem);

                                }
                            }

                            if (cpPhanBoItem.Tk_6232 != null && cpPhanBoItem.Tk_6232 != 0)
                            {
                                int result = IsExistInListTaiKhoan(cpPhanBoItem.DoiTuongTHCPId.ToString(), cpPhanBoItem.HangHoaId.ToString(), cpPhanBoItem.KyGiaThanhId.ToString(), "Kết chuyển 6232");

                                if (result == -1)
                                {
                                    List<string> listitem = new List<string>();
                                    listitem.Add(cpPhanBoItem.DoiTuongTHCPId.ToString());
                                    listitem.Add(cpPhanBoItem.HangHoaId.ToString());
                                    listitem.Add(cpPhanBoItem.KyGiaThanhId.ToString());
                                    listitem.Add(cpPhanBoItem.Tk_6232.ToString());
                                    listitem.Add("Kết chuyển 6232");
                                    listTaiKhoan.Add(listitem);

                                }
                                else
                                {
                                    List<string>[] arraylistTaiKhoan = listTaiKhoan.ToArray();
                                    string[] arrayChiTietListTaiKhoan = arraylistTaiKhoan[result].ToArray();
                                    double SoTienMoi = cpPhanBoItem.Tk_6232.Value + double.Parse(arrayChiTietListTaiKhoan[3]);
                                    listTaiKhoan.RemoveAt(result);

                                    List<string> listitem = new List<string>();
                                    listitem.Add(cpPhanBoItem.DoiTuongTHCPId.ToString());
                                    listitem.Add(cpPhanBoItem.HangHoaId.ToString());
                                    listitem.Add(cpPhanBoItem.KyGiaThanhId.ToString());
                                    listitem.Add(SoTienMoi.ToString());
                                    listitem.Add("Kết chuyển 6232");
                                    listTaiKhoan.Add(listitem);

                                }
                            }
                            if (cpPhanBoItem.Tk_6233 != null && cpPhanBoItem.Tk_6233 != 0)
                            {
                                int result = IsExistInListTaiKhoan(cpPhanBoItem.DoiTuongTHCPId.ToString(), cpPhanBoItem.HangHoaId.ToString(), cpPhanBoItem.KyGiaThanhId.ToString(), "Kết chuyển 6233");

                                if (result == -1)
                                {
                                    List<string> listitem = new List<string>();
                                    listitem.Add(cpPhanBoItem.DoiTuongTHCPId.ToString());
                                    listitem.Add(cpPhanBoItem.HangHoaId.ToString());
                                    listitem.Add(cpPhanBoItem.KyGiaThanhId.ToString());
                                    listitem.Add(cpPhanBoItem.Tk_6233.ToString());
                                    listitem.Add("Kết chuyển 6233");
                                    listTaiKhoan.Add(listitem);

                                }
                                else
                                {
                                    List<string>[] arraylistTaiKhoan = listTaiKhoan.ToArray();
                                    string[] arrayChiTietListTaiKhoan = arraylistTaiKhoan[result].ToArray();
                                    double SoTienMoi = cpPhanBoItem.Tk_6233.Value + double.Parse(arrayChiTietListTaiKhoan[3]);
                                    listTaiKhoan.RemoveAt(result);

                                    List<string> listitem = new List<string>();
                                    listitem.Add(cpPhanBoItem.DoiTuongTHCPId.ToString());
                                    listitem.Add(cpPhanBoItem.HangHoaId.ToString());
                                    listitem.Add(cpPhanBoItem.KyGiaThanhId.ToString());
                                    listitem.Add(SoTienMoi.ToString());
                                    listitem.Add("Kết chuyển 6233");
                                    listTaiKhoan.Add(listitem);

                                }
                            }
                            if (cpPhanBoItem.Tk_6234 != null && cpPhanBoItem.Tk_6234 != 0)
                            {
                                int result = IsExistInListTaiKhoan(cpPhanBoItem.DoiTuongTHCPId.ToString(), cpPhanBoItem.HangHoaId.ToString(), cpPhanBoItem.KyGiaThanhId.ToString(), "Kết chuyển 6234");

                                if (result == -1)
                                {
                                    List<string> listitem = new List<string>();
                                    listitem.Add(cpPhanBoItem.DoiTuongTHCPId.ToString());
                                    listitem.Add(cpPhanBoItem.HangHoaId.ToString());
                                    listitem.Add(cpPhanBoItem.KyGiaThanhId.ToString());
                                    listitem.Add(cpPhanBoItem.Tk_6234.ToString());
                                    listitem.Add("Kết chuyển 6234");
                                    listTaiKhoan.Add(listitem);

                                }
                                else
                                {
                                    List<string>[] arraylistTaiKhoan = listTaiKhoan.ToArray();
                                    string[] arrayChiTietListTaiKhoan = arraylistTaiKhoan[result].ToArray();
                                    double SoTienMoi = cpPhanBoItem.Tk_6234.Value + double.Parse(arrayChiTietListTaiKhoan[3]);
                                    listTaiKhoan.RemoveAt(result);

                                    List<string> listitem = new List<string>();
                                    listitem.Add(cpPhanBoItem.DoiTuongTHCPId.ToString());
                                    listitem.Add(cpPhanBoItem.HangHoaId.ToString());
                                    listitem.Add(cpPhanBoItem.KyGiaThanhId.ToString());
                                    listitem.Add(SoTienMoi.ToString());
                                    listitem.Add("Kết chuyển 6234");
                                    listTaiKhoan.Add(listitem);

                                }
                            }
                            if (cpPhanBoItem.Tk_6237 != null && cpPhanBoItem.Tk_6237 != 0)
                            {
                                int result = IsExistInListTaiKhoan(cpPhanBoItem.DoiTuongTHCPId.ToString(), cpPhanBoItem.HangHoaId.ToString(), cpPhanBoItem.KyGiaThanhId.ToString(), "Kết chuyển 6237");

                                if (result == -1)
                                {
                                    List<string> listitem = new List<string>();
                                    listitem.Add(cpPhanBoItem.DoiTuongTHCPId.ToString());
                                    listitem.Add(cpPhanBoItem.HangHoaId.ToString());
                                    listitem.Add(cpPhanBoItem.KyGiaThanhId.ToString());
                                    listitem.Add(cpPhanBoItem.Tk_6237.ToString());
                                    listitem.Add("Kết chuyển 6237");
                                    listTaiKhoan.Add(listitem);

                                }
                                else
                                {
                                    List<string>[] arraylistTaiKhoan = listTaiKhoan.ToArray();
                                    string[] arrayChiTietListTaiKhoan = arraylistTaiKhoan[result].ToArray();
                                    double SoTienMoi = cpPhanBoItem.Tk_6237.Value + double.Parse(arrayChiTietListTaiKhoan[3]);
                                    listTaiKhoan.RemoveAt(result);

                                    List<string> listitem = new List<string>();
                                    listitem.Add(cpPhanBoItem.DoiTuongTHCPId.ToString());
                                    listitem.Add(cpPhanBoItem.HangHoaId.ToString());
                                    listitem.Add(cpPhanBoItem.KyGiaThanhId.ToString());
                                    listitem.Add(SoTienMoi.ToString());
                                    listitem.Add("Kết chuyển 6237");
                                    listTaiKhoan.Add(listitem);

                                }
                            }
                            if (cpPhanBoItem.Tk_6238 != null && cpPhanBoItem.Tk_6238 != 0)
                            {
                                int result = IsExistInListTaiKhoan(cpPhanBoItem.DoiTuongTHCPId.ToString(), cpPhanBoItem.HangHoaId.ToString(), cpPhanBoItem.KyGiaThanhId.ToString(), "Kết chuyển 6238");

                                if (result == -1)
                                {
                                    List<string> listitem = new List<string>();
                                    listitem.Add(cpPhanBoItem.DoiTuongTHCPId.ToString());
                                    listitem.Add(cpPhanBoItem.HangHoaId.ToString());
                                    listitem.Add(cpPhanBoItem.KyGiaThanhId.ToString());
                                    listitem.Add(cpPhanBoItem.Tk_6238.ToString());
                                    listitem.Add("Kết chuyển 6238");
                                    listTaiKhoan.Add(listitem);

                                }
                                else
                                {
                                    List<string>[] arraylistTaiKhoan = listTaiKhoan.ToArray();
                                    string[] arrayChiTietListTaiKhoan = arraylistTaiKhoan[result].ToArray();
                                    double SoTienMoi = cpPhanBoItem.Tk_6238.Value + double.Parse(arrayChiTietListTaiKhoan[3]);
                                    listTaiKhoan.RemoveAt(result);

                                    List<string> listitem = new List<string>();
                                    listitem.Add(cpPhanBoItem.DoiTuongTHCPId.ToString());
                                    listitem.Add(cpPhanBoItem.HangHoaId.ToString());
                                    listitem.Add(cpPhanBoItem.KyGiaThanhId.ToString());
                                    listitem.Add(SoTienMoi.ToString());
                                    listitem.Add("Kết chuyển 6238");
                                    listTaiKhoan.Add(listitem);

                                }
                            }

                            if (cpPhanBoItem.Tk_6271 != null && cpPhanBoItem.Tk_6271 != 0)
                            {
                                int result = IsExistInListTaiKhoan(cpPhanBoItem.DoiTuongTHCPId.ToString(), cpPhanBoItem.HangHoaId.ToString(), cpPhanBoItem.KyGiaThanhId.ToString(), "Kết chuyển 6271");

                                if (result == -1)
                                {
                                    List<string> listitem = new List<string>();
                                    listitem.Add(cpPhanBoItem.DoiTuongTHCPId.ToString());
                                    listitem.Add(cpPhanBoItem.HangHoaId.ToString());
                                    listitem.Add(cpPhanBoItem.KyGiaThanhId.ToString());
                                    listitem.Add(cpPhanBoItem.Tk_6271.ToString());
                                    listitem.Add("Kết chuyển 6271");
                                    listTaiKhoan.Add(listitem);

                                }
                                else
                                {
                                    List<string>[] arraylistTaiKhoan = listTaiKhoan.ToArray();
                                    string[] arrayChiTietListTaiKhoan = arraylistTaiKhoan[result].ToArray();
                                    double SoTienMoi = cpPhanBoItem.Tk_6271.Value + double.Parse(arrayChiTietListTaiKhoan[3]);
                                    listTaiKhoan.RemoveAt(result);

                                    List<string> listitem = new List<string>();
                                    listitem.Add(cpPhanBoItem.DoiTuongTHCPId.ToString());
                                    listitem.Add(cpPhanBoItem.HangHoaId.ToString());
                                    listitem.Add(cpPhanBoItem.KyGiaThanhId.ToString());
                                    listitem.Add(SoTienMoi.ToString());
                                    listitem.Add("Kết chuyển 6271");
                                    listTaiKhoan.Add(listitem);

                                }
                            }

                            if (cpPhanBoItem.Tk_6272 != null && cpPhanBoItem.Tk_6272 != 0)
                            {
                                int result = IsExistInListTaiKhoan(cpPhanBoItem.DoiTuongTHCPId.ToString(), cpPhanBoItem.HangHoaId.ToString(), cpPhanBoItem.KyGiaThanhId.ToString(), "Kết chuyển 6272");

                                if (result == -1)
                                {
                                    List<string> listitem = new List<string>();
                                    listitem.Add(cpPhanBoItem.DoiTuongTHCPId.ToString());
                                    listitem.Add(cpPhanBoItem.HangHoaId.ToString());
                                    listitem.Add(cpPhanBoItem.KyGiaThanhId.ToString());
                                    listitem.Add(cpPhanBoItem.Tk_6272.ToString());
                                    listitem.Add("Kết chuyển 6272");
                                    listTaiKhoan.Add(listitem);

                                }
                                else
                                {
                                    List<string>[] arraylistTaiKhoan = listTaiKhoan.ToArray();
                                    string[] arrayChiTietListTaiKhoan = arraylistTaiKhoan[result].ToArray();
                                    double SoTienMoi = cpPhanBoItem.Tk_6272.Value + double.Parse(arrayChiTietListTaiKhoan[3]);
                                    listTaiKhoan.RemoveAt(result);

                                    List<string> listitem = new List<string>();
                                    listitem.Add(cpPhanBoItem.DoiTuongTHCPId.ToString());
                                    listitem.Add(cpPhanBoItem.HangHoaId.ToString());
                                    listitem.Add(cpPhanBoItem.KyGiaThanhId.ToString());
                                    listitem.Add(SoTienMoi.ToString());
                                    listitem.Add("Kết chuyển 6272");
                                    listTaiKhoan.Add(listitem);

                                }
                            }
                            if (cpPhanBoItem.Tk_6273 != null && cpPhanBoItem.Tk_6273 != 0)
                            {
                                int result = IsExistInListTaiKhoan(cpPhanBoItem.DoiTuongTHCPId.ToString(), cpPhanBoItem.HangHoaId.ToString(), cpPhanBoItem.KyGiaThanhId.ToString(), "Kết chuyển 6273");

                                if (result == -1)
                                {
                                    List<string> listitem = new List<string>();
                                    listitem.Add(cpPhanBoItem.DoiTuongTHCPId.ToString());
                                    listitem.Add(cpPhanBoItem.HangHoaId.ToString());
                                    listitem.Add(cpPhanBoItem.KyGiaThanhId.ToString());
                                    listitem.Add(cpPhanBoItem.Tk_6273.ToString());
                                    listitem.Add("Kết chuyển 6273");
                                    listTaiKhoan.Add(listitem);

                                }
                                else
                                {
                                    List<string>[] arraylistTaiKhoan = listTaiKhoan.ToArray();
                                    string[] arrayChiTietListTaiKhoan = arraylistTaiKhoan[result].ToArray();
                                    double SoTienMoi = cpPhanBoItem.Tk_6273.Value + double.Parse(arrayChiTietListTaiKhoan[3]);
                                    listTaiKhoan.RemoveAt(result);

                                    List<string> listitem = new List<string>();
                                    listitem.Add(cpPhanBoItem.DoiTuongTHCPId.ToString());
                                    listitem.Add(cpPhanBoItem.HangHoaId.ToString());
                                    listitem.Add(cpPhanBoItem.KyGiaThanhId.ToString());
                                    listitem.Add(SoTienMoi.ToString());
                                    listitem.Add("Kết chuyển 6273");
                                    listTaiKhoan.Add(listitem);

                                }
                            }

                            if (cpPhanBoItem.Tk_6274 != null && cpPhanBoItem.Tk_6274 != 0)
                            {
                                int result = IsExistInListTaiKhoan(cpPhanBoItem.DoiTuongTHCPId.ToString(), cpPhanBoItem.HangHoaId.ToString(), cpPhanBoItem.KyGiaThanhId.ToString(), "Kết chuyển 6274");

                                if (result == -1)
                                {
                                    List<string> listitem = new List<string>();
                                    listitem.Add(cpPhanBoItem.DoiTuongTHCPId.ToString());
                                    listitem.Add(cpPhanBoItem.HangHoaId.ToString());
                                    listitem.Add(cpPhanBoItem.KyGiaThanhId.ToString());
                                    listitem.Add(cpPhanBoItem.Tk_6274.ToString());
                                    listitem.Add("Kết chuyển 6274");
                                    listTaiKhoan.Add(listitem);

                                }
                                else
                                {
                                    List<string>[] arraylistTaiKhoan = listTaiKhoan.ToArray();
                                    string[] arrayChiTietListTaiKhoan = arraylistTaiKhoan[result].ToArray();
                                    double SoTienMoi = cpPhanBoItem.Tk_6274.Value + double.Parse(arrayChiTietListTaiKhoan[3]);
                                    listTaiKhoan.RemoveAt(result);

                                    List<string> listitem = new List<string>();
                                    listitem.Add(cpPhanBoItem.DoiTuongTHCPId.ToString());
                                    listitem.Add(cpPhanBoItem.HangHoaId.ToString());
                                    listitem.Add(cpPhanBoItem.KyGiaThanhId.ToString());
                                    listitem.Add(SoTienMoi.ToString());
                                    listitem.Add("Kết chuyển 6274");
                                    listTaiKhoan.Add(listitem);

                                }
                            }
                            if (cpPhanBoItem.Tk_6277 != null && cpPhanBoItem.Tk_6277 != 0)
                            {
                                int result = IsExistInListTaiKhoan(cpPhanBoItem.DoiTuongTHCPId.ToString(), cpPhanBoItem.HangHoaId.ToString(), cpPhanBoItem.KyGiaThanhId.ToString(), "Kết chuyển 6277");

                                if (result == -1)
                                {
                                    List<string> listitem = new List<string>();
                                    listitem.Add(cpPhanBoItem.DoiTuongTHCPId.ToString());
                                    listitem.Add(cpPhanBoItem.HangHoaId.ToString());
                                    listitem.Add(cpPhanBoItem.KyGiaThanhId.ToString());
                                    listitem.Add(cpPhanBoItem.Tk_6277.ToString());
                                    listitem.Add("Kết chuyển 6277");
                                    listTaiKhoan.Add(listitem);

                                }
                                else
                                {
                                    List<string>[] arraylistTaiKhoan = listTaiKhoan.ToArray();
                                    string[] arrayChiTietListTaiKhoan = arraylistTaiKhoan[result].ToArray();
                                    double SoTienMoi = cpPhanBoItem.Tk_6277.Value + double.Parse(arrayChiTietListTaiKhoan[3]);
                                    listTaiKhoan.RemoveAt(result);

                                    List<string> listitem = new List<string>();
                                    listitem.Add(cpPhanBoItem.DoiTuongTHCPId.ToString());
                                    listitem.Add(cpPhanBoItem.HangHoaId.ToString());
                                    listitem.Add(cpPhanBoItem.KyGiaThanhId.ToString());
                                    listitem.Add(SoTienMoi.ToString());
                                    listitem.Add("Kết chuyển 6277");
                                    listTaiKhoan.Add(listitem);

                                }
                            }
                            if (cpPhanBoItem.Tk_6278 != null && cpPhanBoItem.Tk_6278 != 0)
                            {
                                int result = IsExistInListTaiKhoan(cpPhanBoItem.DoiTuongTHCPId.ToString(), cpPhanBoItem.HangHoaId.ToString(), cpPhanBoItem.KyGiaThanhId.ToString(), "Kết chuyển 6278");

                                if (result == -1)
                                {
                                    List<string> listitem = new List<string>();
                                    listitem.Add(cpPhanBoItem.DoiTuongTHCPId.ToString());
                                    listitem.Add(cpPhanBoItem.HangHoaId.ToString());
                                    listitem.Add(cpPhanBoItem.KyGiaThanhId.ToString());
                                    listitem.Add(cpPhanBoItem.Tk_6278.ToString());
                                    listitem.Add("Kết chuyển 6278");
                                    listTaiKhoan.Add(listitem);

                                }
                                else
                                {
                                    List<string>[] arraylistTaiKhoan = listTaiKhoan.ToArray();
                                    string[] arrayChiTietListTaiKhoan = arraylistTaiKhoan[result].ToArray();
                                    double SoTienMoi = cpPhanBoItem.Tk_6278.Value + double.Parse(arrayChiTietListTaiKhoan[3]);
                                    listTaiKhoan.RemoveAt(result);

                                    List<string> listitem = new List<string>();
                                    listitem.Add(cpPhanBoItem.DoiTuongTHCPId.ToString());
                                    listitem.Add(cpPhanBoItem.HangHoaId.ToString());
                                    listitem.Add(cpPhanBoItem.KyGiaThanhId.ToString());
                                    listitem.Add(SoTienMoi.ToString());
                                    listitem.Add("Kết chuyển 6278");
                                    listTaiKhoan.Add(listitem);

                                }
                            }
                        }
                    }
                }
            }

            foreach (List<string> item in listTaiKhoan)
            {
                this.AddKetChuyenCPDetailsItem();
            }
            this.GetViewModel().CountChanged += new EventHandler(OnCountChanged);
        }

        private int IsExistInListTaiKhoan(string DoiTuongChiPhiId, string HangHoaId, string KyGiaThanhId, string DienGiai)
        {

            int index = 0;
            int result = -1;
            foreach (List<string> item in listTaiKhoan)
            {
                string[] ItemArray = item.ToArray();
                if (ItemArray[0].Equals(DoiTuongChiPhiId) && ItemArray[1].Equals(HangHoaId) && ItemArray[2].Equals(KyGiaThanhId) && ItemArray[4].Equals(DienGiai))
                {
                    result = index;
                    break;
                }
                index++;
            }
            return result;
        }

        
        void OnCountChanged(object sender, EventArgs e)
        {

            System.Windows.Data.CollectionViewSource dm_TaiKhoan_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoan_ListViewModelSource"];
            DM_TaiKhoan_ListViewModel dm_TaiKhoan_ListViewModel = ((List<object>)dm_TaiKhoan_CollectionViewSource.Source)[0] as DM_TaiKhoan_ListViewModel;
            IList<DM_TaiKhoanInfo> listTK = dm_TaiKhoan_ListViewModel.Model.getListOfInfo();
            System.Nullable<int> TKNoId = null;
            foreach (DM_TaiKhoanInfo taikhoanItem in listTK)
            {
                if (taikhoanItem.Ma.Equals("1541"))
                {
                    TKNoId = taikhoanItem.Khoa;
                }
            }
            IList<GT_KetChuyenCP_ChiTietChild> listChiTiet = GetViewModel().Model.GT_KetChuyenCP_ChiTiets;
            if (this.GetViewModel().Model.GT_KetChuyenCP_ChiTiets.Count == listTaiKhoan.Count)
            {
                int index = 0;
                List<string>[] arrayListTaiKhoan = listTaiKhoan.ToArray();
                foreach (GT_KetChuyenCP_ChiTietChild item in listChiTiet)
                {
                    string[] arrayListTaiKhoanChiTiet = arrayListTaiKhoan[index].ToArray();
                    item.DoiTuongTHCPId = int.Parse(arrayListTaiKhoanChiTiet[0]);
                    item.HangHoaId = int.Parse(arrayListTaiKhoanChiTiet[1]);
                    item.SoTien = double.Parse(arrayListTaiKhoanChiTiet[3]);
                    item.DienGiai = arrayListTaiKhoanChiTiet[4];
                    //item.TkNoId = TKNoId.Value;
                    item.TkNoId = 1;
                    string[] rawDataDienGiai = item.DienGiai.Split(' ');

                    foreach (DM_TaiKhoanInfo taikhoanItem in listTK)
                    {
                        if (taikhoanItem.Ma.Equals(rawDataDienGiai[2]))
                        {
                            item.TKCoId = taikhoanItem.Khoa;
                        }
                    }
                    index++;

                }
               
            }
           
        }

        

        void GT_KetChuyenCP_ViewModel_ChildList_AddedNew(object sender, ChildListAddedNewArgs e)
        {
            if (e.ChildListName == "GT_KetChuyenCP_ChiTiets")
            {
                this.GT_KetChuyenCP_KetChuyenCPChiTietGridView.OnAddingNewDataItem(new GridViewAddingNewEventArgs(GetActiveGridView()));
            }
        }

       
        #endregion
        
    }
}
